/**
 * 
 */
package com.sx.form.ts.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sx.article.entity.AttachmentEntity;
import com.sx.core.BaseDao;
import com.sx.feesunit.exception.SXException;
import com.sx.form.ts.dao.IHouseInfoDao;
import com.sx.form.ts.entity.HouseInfoAndAttachment;
import com.sx.form.ts.entity.HouseInfoAndRegion;
import com.sx.form.ts.entity.HouseInfoAndSpecial;
import com.sx.form.ts.entity.HouseInfoEntity;

/**
 * @author Administrator
 *
 */
public class HouseInfoDao extends BaseDao implements IHouseInfoDao {
	private Log log = LogFactory.getLog(this.getClass());
	
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#deleteHouseInfoById(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public boolean deleteHouseInfoById(HouseInfoEntity houseinfo) {
		boolean flag = false;
		try {
			flag = super.delInfo(houseinfo,"HouseInfo.deleteHouseInfoById");
		} catch (Exception e) {
			log.error("按编号删除房源信息",e);
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#deleteHouseInfoAndKeyImageInfo(java.lang.Integer)
	 */
	public boolean deleteHouseInfoAndKeyImageInfo(Integer fid) {
		boolean flag = false;
		try {
			flag = super.delInfo(fid,"HouseInfo.deleteAttachmentAndKeyImageByFileId");
		} catch (Exception e) {
			log.error("按附件编号删除房源与KeyImage关联信息",e);
		}
		return flag;
	}
	/**
	 *按附件编号删除房源与附件关联信息 
	 **/
	public boolean deleteHouseInfoAndAttachmentInfo(Integer fid){
		boolean flag = false;
		try {
			flag = super.delInfo(fid,"HouseInfo.deleteHouseInfoAndAttachmentInfo");
		} catch (Exception e) {
			log.error("按附件编号删除房源与附件关联信息",e);
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#getHouseInfoById(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public HouseInfoEntity getHouseInfoById(HouseInfoEntity houseinfo) {
		HouseInfoEntity house = null;
		try {
			house =(HouseInfoEntity)getSqlMapClientTemplate().queryForObject("HouseInfo.getHouseInfoById", houseinfo);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按编号查询房源信息",e);
		}
		return house;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#findHouseInfo(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public List<HouseInfoEntity> findHouseInfo(HouseInfoEntity houseinfo,String queryinfo){
		List<HouseInfoEntity> list = null;
		try {
			list = super.getSqlMapClientTemplate().queryForList(queryinfo, houseinfo);
		} catch (Exception e) {
			log.error("获取房源数据信息",e);
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#findHouseInfo(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public List<HouseInfoEntity> findHouseInfo(HouseInfoEntity houseinfo){
		List<HouseInfoEntity> list = null;
		try {
			list = super.getSqlMapClientTemplate().queryForList("HouseInfo.findHouseInfo", houseinfo);
		} catch (Exception e) {
			log.error("获取房源数据信息",e);
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#findHouseInfoCount(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public Integer findHouseInfoCount(HouseInfoEntity houseinfo){
		Integer count = null;
		try {
			count = (Integer)super.getSqlMapClientTemplate().queryForObject("HouseInfo.findHouseInfoCount",houseinfo);
		} catch (Exception e) {
			log.error("获取房源数量",e);
		}
		return count;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoCascadeRegion(com.sx.form.ts.entity.HouseInfoAndRegion)
	 */
	public Integer addHouseInfoCascadeRegion(HouseInfoEntity houseinfo) {
		Integer id = null;
		try {
			String [] ids = houseinfo.getRegionid();
			for (String string : ids) {
				if(!string.equals("")){
					String str = string.split("-")[1];
					Integer regionid = Integer.parseInt(str);
					Integer houseid = houseinfo.getId();
					HouseInfoAndRegion hiar = new HouseInfoAndRegion(regionid,houseid);
					addInfo(hiar,"HouseInfo.addHouseInfoCascadeRegion");
				}
			}
			id = new Integer(1);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("添加房源与文件信息关联",e);
		}
		return id;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoCascadeFileIdInfo(com.sx.form.ts.entity.HouseInfoAndAttachment)
	 */
	public Integer addHouseInfoCascadeFileIdInfo(HouseInfoAndAttachment hiaa) {
		Integer id = null;
		try {
			id = addInfo(hiaa,"HouseInfo.addHouseInfoCascadeFileIdInfo");
		} catch (Exception e) {
			// TODO: handle exception
			log.error("添加房源与文件信息关联",e);
		}
		return id;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoDraft(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public Integer addHouseInfoDraft(HouseInfoEntity houseinfo) {
		Integer id = null;
		try {
			id = super.addInfo(houseinfo, "HouseInfo.addHouseInfoDraft");
		} catch (Exception e) {
			// TODO: handle exception
			log.error("暂存房源信息",e);
		}
		return id;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoCascadeFileIdInfo(com.sx.form.ts.entity.HouseInfoEntity, java.util.List)
	 */
	public boolean addHouseInfoCascadeFileIdInfo(HouseInfoEntity houseinfo,
			List<AttachmentEntity> attachments) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			Integer [] ids = houseinfo.getAttachmentid();
			for (Integer id : ids) {
				HouseInfoAndAttachment hiaa = new HouseInfoAndAttachment(houseinfo.getId(),id);
				addInfo(hiaa,"HouseInfo.addHouseInfoCascadeFileIdInfo");
			}
			flag = true;
		} catch (Exception e) {
			log.error("批量为房源添加附件关联信息",e);
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoCascadeKeyImageInfo(com.sx.form.ts.entity.HouseInfoEntity, com.sx.article.entity.AttachmentEntity)
	 */
	public boolean addHouseInfoCascadeKeyImageInfo(HouseInfoEntity houseinfo,
			AttachmentEntity keyimage) {
		boolean flag = false;
		try {
			HouseInfoAndAttachment hiaa = new HouseInfoAndAttachment(houseinfo.getId(),houseinfo.getKeyimageid());
			addInfo(hiaa,"HouseInfo.addHouseInfoCascadeKeyImageInfo");
			flag = true;
		} catch (Exception e) {
			log.error("房源添加关键图片关联信息",e);
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#addHouseInfoCascadeSpecial(com.sx.form.ts.entity.HouseInfoEntity, java.lang.Integer[])
	 */
	public boolean addHouseInfoCascadeSpecial(HouseInfoEntity houseinfo,Integer[] ids) {
		boolean flag = false;
		try {
			for (Integer id : ids) {
				HouseInfoAndSpecial hias = new HouseInfoAndSpecial(houseinfo.getId(),id);
				addInfo(hias,"HouseInfo.addHouseInfoCascadeSpecial");
			}
			flag = true;
		} catch (Exception e) {
			log.error("批量为房源添加房屋特色关联信息",e);
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.dao.IHouseInfoDao#editHouseInfo(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public boolean editHouseInfo(HouseInfoEntity houseinfo) {
		boolean flag = false;
		try {
			flag = super.updateInfo(houseinfo, "HouseInfo.editHouseInfo");
		} catch (Exception e) {
			log.error("更新房源信息",e);
		}
		return flag;
	}

}
